﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Tb.App.API.Applications.Command.Cultivation;
using Tb.App.Domain.Rabbit.Cultivation;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Cultivation
{
    /// <summary>
    /// 养殖企业信息查询-命令
    /// </summary>
    public class GetCultivationCommpanyCommandHandler : IRequestHandler<GetCultivationCommpanyCommand, ApiResult<CultivationCommpanyModel>>
    {
        private readonly IBaseRepository<CultivationCommpanyModel> commpanyRep;

        public GetCultivationCommpanyCommandHandler(IBaseRepository<CultivationCommpanyModel> commpanyRep)
        {
            this.commpanyRep = commpanyRep;
        }
        public async Task<ApiResult<CultivationCommpanyModel>> Handle(GetCultivationCommpanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<CultivationCommpanyModel> result = new ApiResult<CultivationCommpanyModel>();

            var info =await commpanyRep.GetAll().Where(x => !x.IsDel && x.Batch == request.Batch).OrderByDescending(x => x.Id).FirstOrDefaultAsync();
            if (info == null)
            {
                result.Code = ApiEnum.查询失败;
                result.Message = ApiEnum.查询失败.ToString();
            }
            else 
            {
                result.Code = ApiEnum.查询成功;
                result.Message = ApiEnum.查询成功.ToString();
                result.Data = info;
            }
            return await Task.FromResult(result);
        }
    }
}
